<template>
  <div class="edit-popup">
    <!-- 我的频道 -->
    <!-- 标题 -->
    <van-cell title="我的频道">
      <van-button
        size="small"
        round
        class="edit-btn"
        @click="isEdit = !isEdit"
        >{{ isEdit ? '完成' : '编辑' }}</van-button
      >
    </van-cell>
    <!-- 频道 -->
    <div class="my-pannel">
      <van-grid :border="false">
        <van-grid-item
          :text="itms.name"
          v-for="(itms, index) in myChinons"
          :key="itms.id"
          @click="chainnerdele(itms, index)"
          :class="{ active: constList.includes(itms.name) }"
        >
          <template #icon v-if="isEdit && !constList.includes(itms.name)">
            <van-icon name="cross" />
          </template>
        </van-grid-item>
      </van-grid>
    </div>
    <!-- 推荐频道 -->
    <!-- 标题 -->
    <van-cell title="频道推荐"></van-cell>

    <div class="recommend-pannel">
      <van-grid :border="false">
        <van-grid-item
          :text="itms.name"
          v-for="itms in addchinne"
          :key="itms.id"
          @click="$emit('addchinnel',itms)"
        >
          <template #icon>
            <van-icon name="plus" />
          </template>
        </van-grid-item>
      </van-grid>
    </div>
  </div>
</template>

<script>
import { getAllchanneApi } from '@/api'
export default {
  name: 'Mychinons',
  data() {
    return {
      isEdit: false,
      constList: ['推荐'],
      channelsList: []
    }
  },
  created() {
    this.getAllchanne()
  },
  methods: {
    async getAllchanne() {
      const {
        data: { data }
      } = await getAllchanneApi()
      this.channelsList = data.channels
    },
    //删除或者跳转
    chainnerdele({ id, name }, index) {
      if (this.isEdit) {
        if (name !== this.constList[0]) {
          this.$emit('deleMychinner', id)
        }
      } else {
        this.$emit('activunp', index)
      }
    }
  },
  props: {
    myChinons: {
      type: Array,
      require: true
    }
  },
  computed: {
    addchinne() {
      const aa = (id) => this.myChinons.find((itms) => !!(itms.id === id))
      return this.channelsList.filter((itms) => !aa(itms.id))
    }
  }
}
</script>

<style scoped lang="less">
.edit-popup {
  padding-top: 1.333rem;
  .edit-btn {
    border-color: red;
    color: red;
    width: 1.38667rem;
    height: 0.64rem;
  }
}
:deep(.van-grid-item__content) {
  width: 175px;
  height: 86px;
  margin: 0 auto 10px;
  background-color: #f4f5f6;
}
.my-pannel {
  // 编辑按钮
  .editBtn {
    width: 104px;
    height: 48px;
    border-color: red;
    color: red;
  }

  // 编辑按钮居中
  .van-cell__value {
    display: flex;
    justify-content: flex-end;
    align-items: center;
  }

  // 关闭按钮样式
  :deep(.van-grid-item__content) {
    position: relative;

    .van-grid-item__icon-wrapper {
      position: absolute;
      top: 0;
      right: 0;

      .van-icon-cross {
        position: absolute;
        top: 0;
        right: 0;
        transform: translate(20%, -35%);
        border: 0.02667rem solid #000;
        border-radius: 50%;
        text-align: center;
        line-height: 0.32rem;
      }
    }
  }
}
.active {
  :deep(.van-grid-item__text) {
    color: red;
  }
}
// 推荐频道
.recommend-pannel {
  // 推荐频道加号样式
  :deep(.van-grid-item__content) {
    flex-direction: row;

    .van-grid-item__icon {
      font-size: 0.5rem;
    }

    .van-grid-item__text {
      margin-top: 0;
    }
  }
}
</style>
